
#include <iostream>

using namespace std;
const int N = 50, M = 210;
int v[N], w[N];
int dp[210];

int main()
{
    int n, m;
    cin >> m >> n;
    for(int i = 1; i <= n; i++) cin >> w[i] >> v[i];

    for(int i = 1; i <= n; i++)
        for(int j = m; j >= w[i]; j--)
            dp[j] = max(dp[j], dp[j - w[i]] + v[i]);

    cout << dp[m] << endl;
    return 0;
}

